package com.green.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.system.common.Constant;
import com.green.system.common.DataGridView;
import com.green.system.domain.Menu;
import com.green.system.mapper.MenuMapper;
import com.green.system.mapper.RoleMapper;
import com.green.system.service.MenuService;
import com.green.system.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2021/10/17/21:26
 * @Description: 
 */


@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{

    @Autowired
    private MenuMapper menuMapper ;

    @Autowired
    private RoleMapper roleMapper;


    /**
     * id 用户ID 不要做联表查询，因为负载均衡会分开表进行存储，一个表一个表查
     * @param id
     * @return
     */
    @Override
    public List<Menu> queryMenusForListByUserId(Integer id) {
        //根据userId查询角色id集合
        List<Integer> roleIds = this.roleMapper.queryRoleIdsByUserId(id);

        //根据角色ID的集合，查询菜单的ID的集合
        if (roleIds!=null&&roleIds.size()>0){
            List<Integer> menuIds = this.roleMapper.queryMenuIdsByRids(roleIds);
            if (null != menuIds && menuIds.size()>0){
                QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("available",Constant.AVAILABLE_TRUE);
                queryWrapper.and(new Consumer<QueryWrapper<Menu>>(){
                    @Override
                    public void accept(QueryWrapper<Menu> menuQueryWrapper) {
                        menuQueryWrapper.eq("type",Constant.MENU_TYPE_TOP).or()
                                .eq("type",Constant.MENU_TYPE_LEFT);
                    }
                });
                queryWrapper.in("id",menuIds);
                queryWrapper.orderByAsc("ordernum");
                List<Menu> menus = this.menuMapper.selectList(queryWrapper);
                return menus;
            }else {
                return new ArrayList<>();
            }
        }else{
            return new ArrayList<>();
        }
    }

    @Override
    public List<Menu> queryAllMenuForList() {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        qw.and(new Consumer<QueryWrapper<Menu>>() {
            @Override
            public void accept(QueryWrapper<Menu> menuQueryWrapper) {
                menuQueryWrapper.eq("type",Constant.MENU_TYPE_TOP)
                        .or().eq("type",Constant.MENU_TYPE_LEFT);
            }
        });
        qw.orderByAsc("ordernum");
        return this.menuMapper.selectList(qw);
    }

    @Override
    public DataGridView queryAllMenu(MenuVo menuVo) {

        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq(menuVo.getAvailable()!=null,"available",menuVo.getAvailable());
        qw.orderByAsc("ordernum");
        List<Menu> menus = this.menuMapper.selectList(qw);
        return new DataGridView(Long.valueOf(menus.size()),menus);
    }

    @Override
    public Integer queryMenuMaxOrderNum() {
        return this.menuMapper.queryMenuMaxOrderNum();
    }

    @Override
    public Menu saveMenu(Menu menu) {
        this.menuMapper.insert(menu);
        return menu;
    }

    @Override
    public Menu updateMenu(Menu menu) {
        this.menuMapper.updateById(menu);
        return menu;
    }

    @Override
    public Integer queryMenuChildrenCountById(Integer id) {
        return this.menuMapper.queryMenuChildrenCountById(id);
    }

    @Override
    public List<String> queryPermissionCodesByUserId(Integer id) {
        //根据userId查询角色id集合
        List<Integer> roleIds = this.roleMapper.queryRoleIdsByUserId(id);

        //根据角色ID的集合，查询菜单的ID的集合
        if (roleIds!=null&&roleIds.size()>0){
            List<Integer> menuIds = this.roleMapper.queryMenuIdsByRids(roleIds);
            if (null != menuIds && menuIds.size()>0){
                QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("available",Constant.AVAILABLE_TRUE);
                queryWrapper.eq("type",Constant.MENU_TYPE_PERMISSION);
                queryWrapper.in("id",menuIds);
                queryWrapper.orderByAsc("ordernum");
                List<Menu> menus = this.menuMapper.selectList(queryWrapper);
                List<String> permissions = new ArrayList<>();
                for (Menu menu : menus) {
                    permissions.add(menu.getTypecode());
                }
                return permissions;
            }else {
                return null;
            }
        }else{
            return null;
        }
    }
}
